﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Diagnostics;
using System.Data;
using System.Xml.XPath;
using System.Globalization;

namespace WebApp.secure_pages.Xml
{
    public class XMLReader
    {
        public DateTime m_StartDate { get; set; }
        public DateTime m_EndDate { get; set; }

        public WebApp.Event m_Event1 { get; set; }
        public WebApp.Event m_Event2 { get; set; }


        public XMLReader(XDocument xmlDoc)
        {
            //sætter sammanhængene i de vital signs de skal gemmes
            // da der er 2 data opsamlinger, i xml filen, har vi lavet 2 events og gemmer dem i          

            m_Event2 = new Event();
            m_Event2.VitalSign = new VitalSign();

            m_Event1 = new Event();
            m_Event1.VitalSign = new VitalSign();

            //her anvender vi ling to xml til at hente data ud
            //henter det første niveau ud af xml filen
            List<CtVital> ctVital = (from c in xmlDoc.Descendants("citizenvitalsign")
                // her laver vi en annonym klasse til at hente datane ud i
                select new CtVital
                {
                    CitizenID = c.Element("citizenid").Value,
                    StartDate = c.Element("logopendate").Value,
                    EndDate = c.Element("logexpiredate").Value,
                }).ToList<CtVital>();

            //gemmer data i de dertilhørende objecter
            //det skal bemærkes at de oprindelige data ikke var formateret 
            //som en long <citizenid>v56456</citizenid> - men da databasen gerne 
            //vil have en long har vi lavet det til en sådan ved at slette v´et 
            // og rettet ID´et til et der findes i vores database
            m_Event1.VitalSign.CitizenID = long.Parse(ctVital[0].CitizenID);
            m_Event1.PatientID = long.Parse(ctVital[0].CitizenID);

            m_Event2.VitalSign.CitizenID = long.Parse(ctVital[0].CitizenID);
            m_Event2.PatientID = long.Parse(ctVital[0].CitizenID);

            m_StartDate = DateTime.Parse(ctVital[0].StartDate);
            m_EndDate = DateTime.Parse(ctVital[0].EndDate);

            //henter næste niveau ud af xml filen
            List<Vsigns> vSignsData = (from c in xmlDoc.Descendants("vitalsigns")
                                     select new Vsigns
                                     {
                                         Vitalsigndate = c.Element("vitalsigndate").Value,
                                         Vitalsignstime = c.Element("vitalsignstime").Value,
                                     }).ToList<Vsigns>();

            // Parse date and time with custom specifier.
            var dateString1 = vSignsData[0].Vitalsigndate + " " + vSignsData[0].Vitalsignstime;
            var dateString2 = vSignsData[1].Vitalsigndate + " " + vSignsData[1].Vitalsignstime;
            var format = "yyyy-MM-dd h:mm:ss";
            CultureInfo provider = CultureInfo.InvariantCulture;
                
            try
            {
                m_Event1.TimeCreated = DateTime.ParseExact(dateString1, format, provider);
                m_Event2.TimeCreated = DateTime.ParseExact(dateString2, format, provider);
            }
            catch (FormatException)
            {
                //den her rammes hvas det fejler
            }


            //henter næste niveau ud af xml filen
            List<Oximeter> oximeterData = (from c in xmlDoc.Descendants("oximeter")
                                   select new Oximeter
                                   {
                                       HeartRate = int.Parse(c.Element("heartrate").Value),
                                       SaturationLevel = int.Parse(c.Element("saturationlevel").Value),
                                   }).ToList<Oximeter>();

            m_Event1.VitalSign.Oximeter = new Oximeter();
            m_Event1.VitalSign.Oximeter.HeartRate = oximeterData[0].HeartRate;
            m_Event1.VitalSign.Oximeter.SaturationLevel = oximeterData[0].SaturationLevel;

            m_Event2.VitalSign.Oximeter = new Oximeter();
            m_Event2.VitalSign.Oximeter.HeartRate = oximeterData[1].HeartRate;
            m_Event2.VitalSign.Oximeter.SaturationLevel = oximeterData[1].SaturationLevel;

            //henter næste niveau ud af xml filen
            List<Weigth> weigthData = (from c in xmlDoc.Descendants("weigth")
                                     select new Weigth
                                       {
                                           WeightInGrams = int.Parse(c.Element("weigthingrams").Value),
                                       }).ToList<Weigth>();

            m_Event1.VitalSign.Weigth = new Weigth();
            m_Event1.VitalSign.Weigth.WeightInGrams = weigthData[0].WeightInGrams;

            //// ecg - hvad gør man ved en liste
            ////henter næste niveau ud af xml filen
            //List<ECG_list> ecgData = (from c in xmlDoc.Descendants("ecgvalues")
            //                        select new ECG_list
            //                           {
            //                               //6 i den første
            //                               ECGValues11 = int.Parse(c.Element("values").Value),
            //                               ECGValues12 = int.Parse(c.Element("values").Value),
            //                               ECGValues13 = int.Parse(c.Element("values").Value),
            //                               ECGValues14 = int.Parse(c.Element("values").Value),
            //                               ECGValues15 = int.Parse(c.Element("values").Value),
            //                               ECGValues16 = int.Parse(c.Element("values").Value),
            //                               //6 i den næste
            //                               ECGValues21 = int.Parse(c.Element("values").Value),
            //                               ECGValues22 = int.Parse(c.Element("values").Value),
            //                               ECGValues23 = int.Parse(c.Element("values").Value),
            //                               ECGValues24 = int.Parse(c.Element("values").Value),
            //                               ECGValues25 = int.Parse(c.Element("values").Value),
            //                               ECGValues26 = int.Parse(c.Element("values").Value),

            //                           }).ToList<ECG_list>();
            //m_Event1.VitalSign.ECG = new <ECG>();
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues11);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues12);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues13);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues14);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues15);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues16);

            //m_Event2.VitalSign.ECG = new List<ECG>();
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues21);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues22);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues23);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues24);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues25);
            //m_Event1.VitalSign.ECG.Add(ecgData[0].ECGValues26);

            //henter næste niveau ud af xml filen
            List<BlodPressure> bloodData = (from c in xmlDoc.Descendants("bloodpressure")
                                             select new BlodPressure
                                       {
                                           Systolic = int.Parse(c.Element("systolic").Value),
                                           Diastolic = int.Parse(c.Element("diastolic").Value),
                                           HartRate = int.Parse(c.Element("heartrate").Value),
                                       }).ToList<BlodPressure>();

            m_Event1.VitalSign.BlodPressure = new BlodPressure();
            m_Event1.VitalSign.BlodPressure.HartRate = bloodData[0].HartRate;
            m_Event1.VitalSign.BlodPressure.Diastolic = bloodData[0].Diastolic;
            m_Event1.VitalSign.BlodPressure.Systolic = bloodData[0].Systolic;

            m_Event2.VitalSign.BlodPressure = new BlodPressure();
            m_Event2.VitalSign.BlodPressure.HartRate = bloodData[1].HartRate;
            m_Event2.VitalSign.BlodPressure.Diastolic = bloodData[1].Diastolic;
            m_Event2.VitalSign.BlodPressure.Systolic = bloodData[1].Systolic;

        }

    }

    // de klassre data bliver loadet ud i
    public class CtVital
    {
        public string CitizenID { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }

    }

    public class Vsigns
    {
       public string Vitalsigndate { get; set; }
       public string Vitalsignstime { get; set; }
    }

    public class ECG_list 
    { 
         //6 i den første
       public int ECGValues11  { get; set; }
       public int ECGValues12  { get; set; }
       public int ECGValues13  { get; set; }
       public int ECGValues14  { get; set; }
       public int ECGValues15  { get; set; }
       public int ECGValues16  { get; set; }
        //6 i den næste
       public int ECGValues21  { get; set; }
       public int ECGValues22  { get; set; }
       public int ECGValues23  { get; set; }
       public int ECGValues24  { get; set; }
       public int ECGValues25  { get; set; }
       public int ECGValues26  { get; set; }
    }


}